unit Unit13; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  DbCtrls, sqldb, db, Buttons, Unit1;

type

  { TForm13 }

  TForm13 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Datasource1: TDatasource;
    Datasource2: TDatasource;
    DBComboBox1: TDBComboBox;
    DBComboBox2: TDBComboBox;
    Edit1: TEdit;
    Edit10: TEdit;
    Edit11: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Label1: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    SQLQuery1: TSQLQuery;
    SQLQuery2: TSQLQuery;
    SQLQuery3: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    codigo : String;
  end; 

var
  Form13: TForm13; 

implementation

{ TForm13 }

procedure TForm13.FormShow(Sender: TObject);
begin
  Edit1.Text := '';
  Edit2.Text := '';
  DBComboBox1.Text := '';
  DBComboBox2.Text := '';
  Edit3.Text := '';
  Edit6.Text := '';
  Edit7.Text := '';
  Edit8.Text := '';
  Edit4.Text := '';
  Edit9.Text := '';
  Edit10.Text := '';
  Edit11.Text := '';
  
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  if SQLQuery2.Active = True then
    SQLQuery2.Close;

  if SQLQuery3.Active = True then
    SQLQuery3.Close;
  
  if codigo <> '' then
  begin
    SQLQuery1.Database := Form1.PQConnection1;
    SQLQuery1.Transaction := Form1.SQLTransaction1;
    SQLQuery1.SQL.Text := 'select fmp_nombre from familia_producto order by fmp_nombre';

    SQLQuery1.Open;

    DBComboBox1.DataField := 'fmp_nombre';
    DBComboBox1.Clear;

    while not SQLQuery1.EOF do
    begin
      DBComboBox1.Items.Add(SQLQuery1.Fields[0].AsString);
      SQLQuery1.Next;
    end;
    
    SQLQuery2.Database := Form1.PQConnection1;
    SQLQuery2.Transaction := Form1.SQLTransaction1;
    SQLQuery2.SQL.Text := 'select fab_nombre from fabricante order by fab_nombre';

    SQLQuery2.Open;

    DBComboBox2.DataField := 'fab_nombre';
    DBComboBox2.Clear;

    while not SQLQuery2.EOF do
    begin
      DBComboBox2.Items.Add(SQLQuery2.Fields[0].AsString);
      SQLQuery2.Next;
    end;
    
    SQLQuery3.Database := Form1.PQConnection1;
    SQLQuery3.Transaction := Form1.SQLTransaction1;
    SQLQuery3.SQL.Text := 'select * from vw_producto where prd_cdg = ' + codigo;
    SQLQuery3.Open;
    
    while not SQLQuery3.EOF do
    begin
      Edit1.Text := SQLQuery3.Fields[1].AsString;
      Edit2.Text := SQLQuery3.Fields[2].AsString;
      DBComboBox1.Text := SQLQuery3.Fields[3].AsString;
      DBComboBox2.Text := SQLQuery3.Fields[4].AsString;
      Edit3.Text := SQLQuery3.Fields[5].AsString;
      Edit9.Text := SQLQuery3.Fields[6].AsString;
      Edit10.Text := SQLQuery3.Fields[7].AsString;
      Edit11.Text := SQLQuery3.Fields[8].AsString;
      Edit6.Text := SQLQuery3.Fields[9].AsString;
      Edit7.Text := SQLQuery3.Fields[10].AsString;
      Edit4.Text := SQLQuery3.Fields[11].AsString;
      Edit5.Text := SQLQuery3.Fields[12].AsString;
      SQLQuery3.Next;
    end;
    
    SQLQuery3.Close;
    
    Edit8.Text := codigo;
  end;
  
  if (registro.tipo_impresor = 'Hasar P715F') or (registro.tipo_impresor = 'Hasar PR4F')
    or(registro.tipo_impresor = 'Epson TM2000') then
    Edit1.MaxLength := 18;

end;

procedure TForm13.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  if SQLQuery2.Active = True then
    SQLQuery2.Close;

  if SQLQuery3.Active = True then
    SQLQuery3.Close;
end;

procedure TForm13.Button1Click(Sender: TObject);
var
  QueryString : String;

begin
  if(Edit1.Text <> '') and (DBComboBox1.Text <> '') and (DBComboBox2.Text <> '') then
  begin
    QueryString := 'update producto set prd_nombre = ''' + Edit1.Text + ''', prd_descripcion = '''
      + Edit2.Text + ''', prd_familia = (select fmp_cdg from familia_producto where fmp_nombre = '''
      + DBComboBox1.Text + '''), prd_fabricante = (select fab_cdg from fabricante where fab_nombre = '''
      + DBComboBox2.Text + '''), prd_precio1 = ' + Edit3.Text + ', prd_precio2 = ' + Edit9.Text
      + ', prd_precio3 = ' + Edit10.Text + ', prd_precio4 = ' + Edit11.Text + ', prd_stock_minimo = '
      + Edit6.Text + ', prd_stock_maximo = ' + Edit7.Text + ', prd_iva = ' + Edit4.Text
      + ', prd_impint = ' + Edit5.Text + ' where prd_cdg = ' + Edit8.Text;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    ShowMessage('MODIFICACION EXITOSA');

    Close;
  end
  else
    ShowMessage('ERROR EN EL INGRESO DE DATOS');
end;

procedure TForm13.Button2Click(Sender: TObject);
begin
  Close;
end;

initialization
  {$I unit13.lrs}

end.

